package keeto.com.vn.classified.utils;

import java.text.NumberFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import keeto.com.vn.classified.ui.R;
import android.content.res.Resources;
import android.util.Log;

public class StringUtilities {

	private static StringUtilities sInstance = null;
	private static Calendar calendar = Calendar.getInstance();

	public static StringUtilities getInstance() {
		if (sInstance == null) {
			sInstance = new StringUtilities();
		}
		return sInstance;
	}

	private static SimpleDateFormat mFacebookDateFormat = null;
	static {
		mFacebookDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
	}

	public static long ONE_DAY_IN_MS = 24 * 3600 * 1000;
	public static long ONE_WEEK_IN_MM = 7 * ONE_DAY_IN_MS;

	/**
	 * @param dateStr
	 * @return
	 */
	public String formatFacebookDate(String dateStr, Resources res) {

		ParsePosition pos = new ParsePosition(0);
		long then = mFacebookDateFormat.parse(dateStr, pos).getTime();
		long now = new Date().getTime();

		long today = now - (now % ONE_DAY_IN_MS);

		String friendly = null;

		if (then > now) {
			return res.getString(R.string.fb_just_now);
		}

		if (then > today)// in today
		{
			long seconds = (now - then) / 1000;
			long minutes = seconds / 60;
			long hours = minutes / 60;
			long days = hours / 24;

			if (days > 0) {
				if (days == 1) {
					friendly = days + " " + getStringOfTime(1, res);
				} else {
					friendly = days + " " + getStringOfTime(2, res);
				}
			} else if (hours > 0) {
				if (hours == 1) {
					friendly = hours + " " + getStringOfTime(3, res);
				} else {
					friendly = hours + " " + getStringOfTime(4, res);
				}
			} else if (minutes > 0) {
				if (minutes == 1) {
					friendly = minutes + " " + getStringOfTime(5, res);
				} else {
					friendly = minutes + " " + getStringOfTime(6, res);
				}
			} else {
				friendly = res.getString(R.string.fb_about) + " 1 "
						+ getStringOfTime(5, res);
			}
			return friendly + getStringOfTime(0, res) + " "
			+ res.getString(R.string.fb_ago);

		} else if ((today - then) <= ONE_DAY_IN_MS)// Yesterday
		{
			calendar.setTimeInMillis(then);
			long minutes = calendar.get(Calendar.MINUTE);
			long hours = calendar.get(Calendar.HOUR_OF_DAY);
			String time = " " + hours + ":"
					+ ((minutes > 9) ? minutes : ("0" + minutes));
			friendly = getDayofWeekString(-1, res) + time;
			return friendly;
		} else if ((today - then) < ONE_WEEK_IN_MM) {
			/*
			 * SUNDAY: 1 MONDAY: 2 TUEDAY: 3 WEDNESDAY: 4 THURSDAY: 5 FRIDAY: 6
			 * SATURDAY: 7
			 */

			calendar.setTimeInMillis(then);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			String dayAt = getDayofWeekString(dayOfWeek, res);
			long minutes = calendar.get(Calendar.MINUTE);
			long hours = calendar.get(Calendar.HOUR_OF_DAY);
			String time = " " + hours + ":"
					+ ((minutes > 9) ? minutes : ("0" + minutes));
			friendly = dayAt + time;
			return friendly;
		} else // a weeks ago
		{
			calendar.setTimeInMillis(then);
			int monthOfYear = calendar.get(Calendar.MONTH);
			int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
			String dateAt = getStringOfMomth(monthOfYear, dayOfMonth, res);

			long minutes = calendar.get(Calendar.MINUTE);
			long hours = calendar.get(Calendar.HOUR_OF_DAY);
			String time = " " + hours + ":"
					+ ((minutes > 9) ? minutes : ("0" + minutes));
			// date and time
			StringBuffer sb = new StringBuffer();
			sb.append(dateAt);
			sb.append(time);
			return sb.toString();
		}
	}

	public static String formatTwitterDate(Date date, Resources res) {
		long then = date.getTime();
		long now = new Date().getTime();

		String friendly = null;

		if (then > now) {
			return res.getString(R.string.fb_just_now);
		}

		if (now - then <= ONE_DAY_IN_MS)// in today
		{
			long seconds = (now - then) / 1000;
			long minutes = seconds / 60;
			long hours = minutes / 60;
			long days = hours / 24;

			long num = 0;
			if (hours > 0) {
				num = hours;
				if (hours == 1) {
					friendly = hours + " " + getStringOfTwitterTime(2, res);
				} else {
					friendly = hours + " " + getStringOfTwitterTime(3, res);
				}
			} else if (minutes > 0) {
				num = minutes;
				friendly = minutes + " " + getStringOfTwitterTime(1, res) + " ";
			} else {
				friendly = seconds + " " + getStringOfTwitterTime(4, res) + " ";
			}
			return friendly;
		} else {
			calendar.setTimeInMillis(then);
			int monthOfYear = calendar.get(Calendar.MONTH);
			int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
			String dateAt = getStringOfMomth(monthOfYear, dayOfMonth, res);
			return dateAt.replaceAll(res.getString(R.string.fb_at), "");
		}
	}

	/**
	 * @param dateStr
	 * @return
	 */
	public static String formatFacebookDate(long timestamp, Resources res) {
		long then = timestamp;
		long now = new Date().getTime();

		long today = now - (now % ONE_DAY_IN_MS);

		String friendly = null;

		if (then > now) {
			return res.getString(R.string.fb_just_now);
		}

		if (then > today)// in today
		{
			long seconds = (now - then) / 1000;
			long minutes = seconds / 60;
			long hours = minutes / 60;
			long days = hours / 24;

			long num = 0;
			if (days > 0) {
				num = days;
				if (days == 1) {
					friendly = days + " " + getStringOfTime(1, res);
				} else {
					friendly = days + " " + getStringOfTime(2, res);
				}
			} else if (hours > 0) {
				num = hours;
				if (hours == 1) {
					friendly = hours + " " + getStringOfTime(3, res);
				} else {
					friendly = hours + " " + getStringOfTime(4, res);
				}
			} else if (minutes > 0) {
				num = minutes;
				if (minutes == 1) {
					friendly = minutes + " " + getStringOfTime(5, res);
				} else {
					friendly = minutes + " " + getStringOfTime(6, res);
				}
			} else {
				num = seconds;
				friendly = res.getString(R.string.fb_about) + " 1 "
						+ getStringOfTime(5, res);
			}
			return friendly + getStringOfTime(0, res) + " "
			+ res.getString(R.string.fb_ago);

		} else if ((today - then) <= ONE_DAY_IN_MS)// Yesterday
		{
			calendar.setTimeInMillis(then);
			long minutes = calendar.get(Calendar.MINUTE);
			long hours = calendar.get(Calendar.HOUR_OF_DAY);
			String time = " " + hours + ":"
					+ ((minutes > 9) ? minutes : ("0" + minutes));
			friendly = getDayofWeekString(-1, res) + time;
			return friendly;
		} else // 2 or more days ago
		{
			/*
			 * SUNDAY: 1 MONDAY: 2 TUEDAY: 3 WEDNESDAY: 4 THURSDAY: 5 FRIDAY: 6
			 * SATURDAY: 7
			 */
			calendar.setTimeInMillis(then);
			int monthOfYear = calendar.get(Calendar.MONTH);
			int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
			String dateAt = getStringOfMomth(monthOfYear, dayOfMonth, res);
			long minutes = calendar.get(Calendar.MINUTE);
			long hours = calendar.get(Calendar.HOUR_OF_DAY);
			String time = " " + hours + ":"
					+ ((minutes > 9) ? minutes : ("0" + minutes));
			// date and time
			StringBuffer sb = new StringBuffer();
			sb.append(dateAt);
			sb.append(time);
			return sb.toString();
		}
	}

	/**
	 * 
	 * @param day
	 * @return day_of_week at - i.e.,"Mon at " or "星期三 "
	 */
	public static String getDayofWeekString(int day, Resources res) {
		switch (day) {
		case -1: // return yesterday.
			return res.getString(R.string.fb_day_yestoday_at);
		case Calendar.SUNDAY:
			return res.getString(R.string.fb_sunday_at);
		case Calendar.MONDAY:
			return res.getString(R.string.fb_monday_at);
		case Calendar.TUESDAY:
			return res.getString(R.string.fb_tuesday_at);
		case Calendar.WEDNESDAY:
			return res.getString(R.string.fb_wednesday_at);
		case Calendar.THURSDAY:
			return res.getString(R.string.fb_thursday_at);
		case Calendar.FRIDAY:
			return res.getString(R.string.fb_friday_at);
		case Calendar.SATURDAY:
			return res.getString(R.string.fb_saturday_at);
		}
		return "";
	}

	/**
	 * @param dateStr
	 * @return
	 */
	public static String getTimeForMessage(long timeForDisplay, Resources res) {

		long then = timeForDisplay;
		long now = new Date().getTime();

		long today = now - (now % ONE_DAY_IN_MS);

		String friendly = null;

		if (then > now) {
			return res.getString(R.string.fb_just_now);
		}

		if (then > today)// in today
		{
			long seconds = (now - then) / 1000;
			long minutes = seconds / 60;
			long hours = minutes / 60;
			long days = hours / 24;

			calendar.setTimeInMillis(then);
			long hourInDay = calendar.get(Calendar.MINUTE);
			long minutesInHour = calendar.get(Calendar.HOUR_OF_DAY);

			if (days > 0) {
				if (days == 1) {
					friendly = days + " " + getStringOfTime(1, res);
				} else {

					friendly = ""
							+ minutesInHour
							+ ":"
							+ ((hourInDay > 9) ? hourInDay
									: ("0" + hourInDay));
				}
			} else {
				friendly = ""
						+ (minutesInHour == 0 ? "00" : minutesInHour)
						+ ":"
						+ ((hourInDay > 9) ? hourInDay
								: ("0" + hourInDay));
			}
			return friendly;

		} else if ((today - then) <= ONE_DAY_IN_MS)// Yesterday
		{
			calendar.setTimeInMillis(then);
			long minutes = calendar.get(Calendar.MINUTE);
			long hours = calendar.get(Calendar.HOUR_OF_DAY);
			String time = " " + hours + ":"
					+ ((minutes > 9) ? minutes : ("0" + minutes));
			friendly = getDayofWeekString(-1, res) + time;
			return friendly;
		} else if ((today - then) < ONE_WEEK_IN_MM) {
			/*
			 * SUNDAY: 1 MONDAY: 2 TUEDAY: 3 WEDNESDAY: 4 THURSDAY: 5 FRIDAY: 6
			 * SATURDAY: 7
			 */

			calendar.setTimeInMillis(then);
			int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
			String dayAt = getDayofWeekString(dayOfWeek, res);
			long minutes = calendar.get(Calendar.MINUTE);
			long hours = calendar.get(Calendar.HOUR_OF_DAY);
			String time = " " + hours + ":"
					+ ((minutes > 9) ? minutes : ("0" + minutes));
			friendly = dayAt + time;
			return friendly;
		} else // a weeks ago
		{
			calendar.setTimeInMillis(then);
			int monthOfYear = calendar.get(Calendar.MONTH);
			int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
			String dateAt = getStringOfMomth(monthOfYear, dayOfMonth, res);

			long minutes = calendar.get(Calendar.MINUTE);
			long hours = calendar.get(Calendar.HOUR_OF_DAY);
			String time = " " + hours + ":"
					+ ((minutes > 9) ? minutes : ("0" + minutes));
			// date and time
			StringBuffer sb = new StringBuffer();
			sb.append(dateAt);
			sb.append(time);
			Log.e("TIME","TIME :" + timeForDisplay + " >>> " + sb.toString() );
			return sb.toString();
		}
	}

	/**
	 * @param index
	 *            1 - mins 2 - hour 3 - hours
	 * @param res
	 * @return
	 */
	public static String getStringOfTwitterTime(int index, Resources res) {
		switch (index) {
		case 1:
			return res.getString(R.string.tw_mins);
		case 2:
			return res.getString(R.string.tw_hour);
		case 3:
			return res.getString(R.string.tw_hours);
		case 4:
			return res.getString(R.string.fb_sencond);
		}
		return "";
	}

	/**
	 * @param index
	 *            1 - day 2 - days 3 - hour 4 - hours 5 - minute 6 - minutes 7 -
	 *            seconds 8 -
	 * @param res
	 * @return
	 */
	public static String getStringOfTime(int index, Resources res) {
		switch (index) {
		case 1:
			return res.getString(R.string.fb_day);
		case 2:
			return res.getString(R.string.fb_days);
		case 3:
			return res.getString(R.string.fb_hour);
		case 4:
			return res.getString(R.string.fb_hours);
		case 5:
			return res.getString(R.string.fb_minute);
		case 6:
			return res.getString(R.string.fb_minutes);
		case 7:
			return res.getString(R.string.fb_sencond);
		}
		return "";
	}

	public static String getStringOfMomth(int month, int day, Resources res) {
		switch (month) {
		case Calendar.JANUARY:
			return res.getString(R.string.fb_january, " " + day);
		case Calendar.FEBRUARY:
			return res.getString(R.string.fb_february, " " + day);
		case Calendar.MARCH:
			return res.getString(R.string.fb_march, " " + day);
		case Calendar.APRIL:
			return res.getString(R.string.fb_april, " " + day);
		case Calendar.MAY:
			return res.getString(R.string.fb_may, " " + day);
		case Calendar.JUNE:
			return res.getString(R.string.fb_june, " " + day);
		case Calendar.JULY:
			return res.getString(R.string.fb_july, " " + day);
		case Calendar.AUGUST:
			return res.getString(R.string.fb_augest, " " + day);
		case Calendar.SEPTEMBER:
			return res.getString(R.string.fb_september, " " + day);
		case Calendar.OCTOBER:
			return res.getString(R.string.fb_october, " " + day);
		case Calendar.NOVEMBER:
			return res.getString(R.string.fb_november, " " + day);
		case Calendar.DECEMBER:
			return res.getString(R.string.fb_december, " " + day);
		}
		return "";
	}

	/**
	 * 
	 * @param commentsCount
	 * @param likeCount
	 * @param res
	 * @return string - if the comments count > 0, "2 comments", "1 comment"
	 *         </br> if the comments counts is 0, the like counts > 0 should be
	 *         "Comment"
	 */
	public static String getCommentsString(long commentsCount, long likeCount,
			Resources res) {
		if (likeCount > 0) {
			if (commentsCount <= 0) {
				return "";
			} else {
				if (commentsCount == 1) {
					return "" + 1;// +" "+res.getString(R.string.fb_text_comment);
				} else {
					return "" + commentsCount;// +" "+res.getString(R.string.fb_text_comments);
				}
			}
		} else {
			if (commentsCount <= 0) {
				return null;
			} else {
				if (commentsCount == 1) {
					return "" + 1;// +" "+res.getString(R.string.fb_text_comment);
				} else {
					return "" + commentsCount;// +" "+res.getString(R.string.fb_text_comments);
				}
			}
		}
	}

	/**
	 * 
	 * @param likeCount
	 * @param res
	 * @return likeCount == 1 "1 likes this" , likeCount > 2 "2 like this"
	 */
	public static String getLikesString(long likeCount, Resources res) {
		if (likeCount > 0) {
			if (likeCount == 1) {
				return 1 + "";// +" "+ res.getString(R.string.fb_text_likes);
			} else {
				return likeCount + "";// +" "+
				// res.getString(R.string.fb_text_like);
			}
		} else {
			return null;
		}
	}

	public static String getShortTime(long timestamp) {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
		String currentDate = dateformat.format(new Date(System
				.currentTimeMillis()));
		String messageDate = dateformat.format(new Date(timestamp));
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String time = formatter.format(new Date(timestamp));
		if (currentDate.equals(messageDate)) {
			return " " + time + " ";
		} else {
			return "" + messageDate + " " + time + " ";
		}
	}

	public static String getPostFormatTime(long timestamp) {
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		return dateformat.format(new Date(timestamp));

	}

	public static String getHourMinuteTime(long timestamp) {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
		String time = formatter.format(new Date(timestamp));
		return " " + time + " ";
	}

	public static String getCompleteTime(long timestamp) {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm yyyy-MM-dd");
		String messageDate = formatter.format(new Date(timestamp));
		return messageDate;
	}

	public static String formatTimeNumber(int n) {
		if (n < 10) {
			return "0" + n;
		}
		return "" + n;
	}

	private static NumberFormat numberFormat = NumberFormat
			.getInstance(Locale.GERMANY);

	/**
	 * @param number
	 *            the original Rp
	 * @return if the origianl number is like this: 100000000 <br/>
	 *         after format should be like this: "100.000.000,-"
	 */
	public static String formatRpStyleNumber(double number) {

		String formatedNumber = numberFormat.format(number);
		if (formatedNumber.contains(",")) {
			return formatedNumber;
		} else {
			return formatedNumber + ",-";
		}
	}


//	public static String getCurrencyUnitByDefaultLanguge(){
//		if(ReleaseConfig.DEFAULT_LANGUAGE.equals(ReleaseConfig.LOCALE_VIETNAMESE)){
//			return ReleaseConfig.CURRENCY_UNIT_VIETNAM_DONG;
//		}else if(ReleaseConfig.DEFAULT_LANGUAGE.equals(ReleaseConfig.DEFAULT_AREA_INDONESIA)){
//			return ReleaseConfig.CURRENCY_UNIT_INDONESIAN_RUPIAH;
//		}
//		return "";
//	}

	public static String convertPrice(String price) {
		if (price!=null && !price.equals("null") && price.length() > 0) {
			String cleanString = price.toString().replaceAll("[,.]", "");
			double parsed =0;
			try {
				parsed = Double.parseDouble(cleanString.trim());

			} catch (Exception e) {
				// TODO: handle exception
			}

			String formated = NumberFormat.getInstance().format((parsed));
			return formated;
		} else {
			return "";
		}
	}

	public static String convertNewPrice(String price, double discount){
		if(!price.equals("null") && price.length() > 0){
			String cleanString = price.toString()
					.replaceAll("[,.]", "");

			double parsed = Double
					.parseDouble(cleanString);
			parsed = parsed -(parsed*discount)/100;
			String formated = NumberFormat
					.getInstance().format(parsed);
			return formated;
		}else{
			return "";
		}
	}
}
